<?php
class AccessModel extends Model {
	
	// 获取所有信息
	public function getAllAccess($where = '', $field = '*', $limit = '') {
		return $this->where ( $where )->field ( $field )->limit ( $limit )->select ();
	}
	
	// 删除
	public function delAccess($where) {
		if ($where) {
			return $this->where ( $where )->delete ();
		} else {
			return false;
		}
	}
	
	/**
	 * 获取菜单节点表信息
	 *
	 * @param int $node_id
	 *        	菜单节点ID
	 * @param int $node
	 *        	菜单节点数据
	 */
	public function get_nodeinfo($node_id, $node) {
		$info ['node_id'] = $node [$node_id] ['id'];
		$info ['pid'] = $node [$node_id] ['pid'];
		$info ['level'] = $node [$node_id] ['level'];
		return $info;
	}
	
	/**
	 * 检查指定菜单是否有权限
	 *
	 * @param array $node
	 *        	node表中某记录数组
	 * @param int $roleid
	 *        	需要检查的角色ID
	 * @param int $access
	 *        	access表的所有数组记录
	 */
	public function is_checked($node, $roleid, $access) {
		$nodetemp = array (
				'role_id' => $roleid,
				'node_id' => $node ['id'],
				'pid' => $node ['pid'],
				'level' => $node ['level'] 
		);
		$info = in_array ( $nodetemp, $access );
		if ($info) {
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * 获取菜单深度
	 *
	 * @param
	 *        	$id
	 * @param
	 *        	$array
	 * @param
	 *        	$i
	 */
	public function get_level($id, $array = array(), $i = 0) {
		foreach ( $array as $n => $value ) {
			if ($value ['id'] == $id) {
				if ($value ['pid'] == '0')
					return $i;
				$i ++;
				return $this->get_level ( $value ['pid'], $array, $i );
			}
		}
	}
	public function addAll($dataList, $options = array(), $replace = false) {
		if (empty ( $dataList )) {
			$this->error = L ( '_DATA_TYPE_INVALID_' );
			return false;
		}
		// 分析表达式
		$options = $this->_parseOptions ( $options );
		// 数据处理
		foreach ( $dataList as $key => $data ) {
			$dataList [$key] = $this->_facade ( $data );
		}
		// 写入数据到数据库
		$result = $this->db->insertAll ( $dataList, $options, $replace );
		if (false !== $result) {
			$insertId = $this->getLastInsID ();
			if ($insertId) {
				return $insertId;
			}
		}
		return $result;
	}
	protected function _parseOptions($options = array()) {
		if (is_array ( $options ))
			$options = array_merge ( $this->options, $options );
			// 查询过后清空sql表达式组装 避免影响下次查询
		$this->options = array ();
		if (! isset ( $options ['table'] )) {
			// 自动获取表名
			$options ['table'] = $this->getTableName ();
			$fields = $this->fields;
		} else {
			// 指定数据表 则重新获取字段列表 但不支持类型检测
			$fields = $this->getDbFields ();
		}
		
		if (! empty ( $options ['alias'] )) {
			$options ['table'] .= ' ' . $options ['alias'];
		}
		// 记录操作的模型名称
		$options ['model'] = $this->name;
		
		// 字段类型验证
		if (isset ( $options ['where'] ) && is_array ( $options ['where'] ) && ! empty ( $fields ) && ! isset ( $options ['join'] )) {
			// 对数组查询条件进行字段类型检查
			foreach ( $options ['where'] as $key => $val ) {
				$key = trim ( $key );
				if (in_array ( $key, $fields, true )) {
					if (is_scalar ( $val )) {
						$this->_parseType ( $options ['where'], $key );
					}
				} elseif (! is_numeric ( $key ) && '_' != substr ( $key, 0, 1 ) && false === strpos ( $key, '.' ) && false === strpos ( $key, '(' ) && false === strpos ( $key, '|' ) && false === strpos ( $key, '&' )) {
					unset ( $options ['where'] [$key] );
				}
			}
		}
		
		// 表达式过滤
		$this->_options_filter ( $options );
		return $options;
	}
}